//
//  LLTestViewController.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/3.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLTestViewController.h"
#import "DetailNewsView.h"
#import "DetailsToolBar.h"
#import "QZTopTextView.h"
#import "LLLoginViewController.h"
@interface LLTestViewController ()<QZTopTextViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) DetailNewsView *detailNewsView;

@property (nonatomic,strong) DetailsToolBar *detailsToolBar;

@property (nonatomic,strong) UITableView *tableView;

/**
 评论框
 */
@property (nonatomic,strong) QZTopTextView *commentTextView;

@property (nonatomic,strong) NSDictionary *newsDict;

@end

@implementation LLTestViewController
-(QZTopTextView *)commentTextView{
    if (!_commentTextView) {
        _commentTextView = [QZTopTextView new];
        _commentTextView.isNavi = YES;
        _commentTextView.delegate = self;
    }
    return _commentTextView;
}
-(NSDictionary *)newsDict{
    if (!_newsDict) {
        _newsDict = [NSDictionary dictionary];
    }
    return _newsDict;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.redColor;
    self.navigationItem.title = @"商家名称";
    // Do any additional setup after loading the view.
    [self initWindow];
    [self getNewsDetailRequst];
    
//    [self.detailNewsView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view.mas_left);
//         make.right.equalTo(self.view.mas_right);
//         make.top.equalTo(self.view.mas_top);
////         make.bottom.equalTo(self.view.mas_bottom).with.offset(-40);
//    }];
    
    self.detailsToolBar = [[DetailsToolBar alloc]init];
    [self.view addSubview:self.detailsToolBar];
    [self.detailsToolBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.tableView.mas_bottom);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    WeakSelf(weakSelf);
    LLUserInfo *user = [LLUserInfo sharedUser];
    self.detailsToolBar.toolBarBlock = ^(NSInteger type,BOOL isSelected) {
        NSLog(@"%ld",type);
        if (type == 0) {
            if (!user.isLogin) {
                LLLoginViewController *vc = [LLLoginViewController new];
                [weakSelf.navigationController pushViewController:vc animated:YES];
               
            }else{
                [weakSelf.commentTextView.countNumTextView becomeFirstResponder];
            }
            
        }else if(type == 1){
            [weakSelf dianZan:isSelected];
        }else{
            
        }
        
    };
     [self.view addSubview:self.commentTextView];//评论框
  
}

-(void)initWindow{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStyleGrouped];
    //    _tableView.backgroundColor = [UIColor redColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = UIColor.whiteColor;
    _tableView.estimatedSectionHeaderHeight = 70;
//    _tableView.estimatedSectionFooterHeight = 0;
//    _tableView.estimatedRowHeight = 0;
//    _tableView.rowHeight = UITableViewAutomaticDimension; //
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).with.offset(-44);
            }else{
                make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).with.offset(-20);
            }
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).with.offset(-40);
            make.left.equalTo(self.view.mas_safeAreaLayoutGuideLeft);
            make.right.equalTo(self.view.mas_safeAreaLayoutGuideRight);
        } else {
            make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 40, 0));
        }
    }];
    
//    _tableView.tableHeaderView = self.headView;
    //    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
    //
    //    }];
    
    
}
#pragma mark----tableviewdelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    [cell configAccountCell:self.dataArray[indexPath.row]];
    return cell;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//
//    return 65;
//}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 0.0001f;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 0.0001f;
//}

#pragma mark---QZTopTextViewDelegate


- (void)sendComment:(NSString *)comment{

      NSString *commentStr = self.commentTextView.countNumTextView.text;
    if (commentStr.length == 0) {
        [self showSVProgressHUDInfoWithsStatus:@"请输入内容"];
        return;
    }
     [self commitComment:commentStr];
}

#pragma mark---新闻详情页
-(void)getNewsDetailRequst{
    LLUserInfo *user = [LLUserInfo sharedUser];
  
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"14",@"new_id":@"11"}];

    if (user.isLogin) {
        [dic setObject:user.user_id forKey:@"user_id"];
        [dic setObject:user.user_random forKey:@"user_random"];
    }
    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        [SVProgressHUD dismiss];
        self.newsDict = result[@"data"];
        CGFloat contentTextH = [CommonTools sizeOfText:self.newsDict[@"article"] fontSize:15 width:LLScreenWidth-2*18].height;
         CGFloat titleH = [CommonTools sizeOfText:self.newsDict[@"new_title"] fontSize:23 width:LLScreenWidth-2*18].height;
        self.detailNewsView = [[DetailNewsView alloc]initWithFrame:CGRectMake(0, 0, LLScreenWidth, contentTextH+titleH+230)];
        self.tableView.tableHeaderView = self.detailNewsView;
        //    [self.tableView addSubview:self.detailNewsView];
        //    [self.view addSubview:self.detailNewsView];
        [self.detailNewsView.detailSubview.likeBtn addTarget:self action:@selector(guanZhu:) forControlEvents:UIControlEventTouchUpInside];

        [self.detailNewsView configDetailNewsView:self.newsDict];
        [self.tableView reloadData];
        
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}
//点赞
-(void)dianZan:(BOOL)isSelected{
//edu_id:18(固定值)
//new_id: 新闻id
//    user_id; 用户id
//user_random: 随机数    is_love: 1是点赞，0是取消点赞
    NSInteger islove;
    if(isSelected){
        islove = 1;
    }else{
        islove = 0;
    }
    
    LLUserInfo *user = [LLUserInfo sharedUser];
    if (!user.isLogin) {
        LLLoginViewController *vc = [LLLoginViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"17",@"new_id":self.news_id}];
    [dic setObject:user.user_id forKey:@"user_id"];
    [dic setObject:user.user_random forKey:@"user_random"];
     [dic setObject:@(islove) forKey:@"is_love"];
     [dic setObject:@"11" forKey:@"new_id"];
    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
//        [SVProgressHUD dismiss];
        if (isSelected) {
              [self showSVProgressHUDInfoWithsStatus:@"点赞成功"];
        }else{
              [self showSVProgressHUDInfoWithsStatus:@"取消点赞"];
        }
      
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}

/**
 发表评论

 @param comment 内容
 */
-(void)commitComment:(NSString *)comment{
//edu_id:22(固定值)
//user_id: 用户的id
//user_random: 随机数
//comment: 评论内容
     LLUserInfo *user = [LLUserInfo sharedUser];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"22",@"new_id":@"11"}];
    [dic setObject:user.user_id forKey:@"user_id"];
    [dic setObject:user.user_random forKey:@"user_random"];
    [dic setObject:comment forKey:@"comment"];
    [dic setObject:@"11" forKey:@"new_id"];
    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        //        [SVProgressHUD dismiss];
        
        if([result[@"code"] integerValue] == 0){
            [self showSVProgressHUDInfoWithsStatus:@"发表成功"];
        }else{
            [self showSVProgressHUDInfoWithsStatus:result[@"msg"]];
        }
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
}


-(void)guanZhu:(UIButton *)button{
    button.selected = !button.selected;
//edu_id:15(固定值)
//user_id:  用户id
//new_uid:  新闻发布者的id     user_random: 随机数
//is_follow: 1是关注，0是取消关注
    NSInteger islove;
    if(button.selected){
        islove = 1;
    }else{
        islove = 0;
    }
    LLUserInfo *user = [LLUserInfo sharedUser];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"15"}];
    [dic setObject:user.user_id forKey:@"user_id"];
    [dic setObject:user.user_random forKey:@"user_random"];
    [dic setObject:self.newsDict[@"new_uid"] forKey:@"new_uid"];
    [dic setObject:@(islove) forKey:@"is_follow"];
    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        //        [SVProgressHUD dismiss];
        if (islove == 1) {
             [self showSVProgressHUDInfoWithsStatus:@"关注成功"];
        }else{
             [self showSVProgressHUDInfoWithsStatus:@"取消关注"];
        }
       
        
        
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
}

@end
